//
//  BRPointRecordVC.m
//  Kexu
//
//  Created by IOS on 2023/7/28.
//

#import "BRPointRecordVC.h"
#import "YGCategoryView.h"
#import "BRPointRuleTbCell.h"
@interface BRPointRecordVC ()<QMUITableViewDelegate,QMUITableViewDataSource>
{
    NSInteger _offset;
    
}

@property (nonatomic,strong) NSMutableArray *dataList;

@property (nonatomic,strong) YGCategoryCustomView *couponTypeView;

@property (nonatomic,assign) NSInteger selIndex;

@property (nonatomic,strong) UIView *sectionView;

@end

@implementation BRPointRecordVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.dataList = [NSMutableArray array];
    [self setUI];
    
    [self addRefreshActionForView:self.tableView];
    [self addLoadMoreActionForView:self.tableView];
    
    self.tableView.mj_footer.hidden = YES;
    
    [self requePointList];
    
    [self br_blockAction];
}

- (void)handleListRefreshAction {
    _offset = 0;
    [self requePointList];
}

- (void)handleListLoadMoreAction {
    _offset += 20;
    [self requePointList];
}

#pragma mark - request --
- (void)requePointList{
    
    NSDictionary *dic = @{@"offset":@(_offset),@"type":@(self.selIndex + 1)};
    NSString *url = [BRAPI pointHistory];
    [self.tableView ly_startLoading];
    [[BRRequestManager shared] GET:url params:dic succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
        NSLog(@"%@",dataDict);
        if (self->_offset == 0) {
            [self.dataList removeAllObjects];
        }
        self.tableView.mj_footer.hidden = NO;
        NSInteger allC = self->_dataList.count;
        NSArray *list = [BRPointHistoryModel mj_objectArrayWithKeyValuesArray:dataDict[@"data"]];
        [self.dataList addObjectsFromArray:list];
        if (self.dataList.count < allC + kPageSize || self.dataList.count == 0 ) {
            self.tableView.mj_footer.hidden = YES;
        }
        
        [self.tableView reloadData];
        [self showBREmptyView:self.tableView];
        [self handleEndRefreshForView:self.tableView];
        
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        [self showBRErrorView:self.tableView];
        [self handleEndRefreshForView:self.tableView];
    }];
}


#pragma mark - func --

- (void)setSelIndex:(NSInteger)selIndex {
    _selIndex = selIndex;
    
    [self handleListRefreshAction];
}

- (void)br_blockAction {
    
    WS(weakSelf);
    self.couponTypeView.itemSeletedBlock = ^(NSInteger index) {
        weakSelf.selIndex = index;
    };
}



#pragma mark - tableView Delegate --

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return _dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BRPointListTbCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRPointListTbCell"];

    cell.type = self.selIndex;
    cell.dto = self.dataList[indexPath.row];
    
  
  
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}


#pragma mark - scroller --
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
 
    
//    NSLog(@"%.2f",_headView.mj_h);
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat newoffsetY = offsetY;
    CGFloat maxY = 0;
    // 临界值150，向上拖动时变透明
    
    [self br_changSupperView:newoffsetY <= maxY];
}

- (void)br_changSupperView:(BOOL)defaultV {
    
    self.sectionView.hidden = defaultV;
}

#pragma mark - UI --
- (void)setUI {
    
    BRNavView *navV = [self br_createTop:Localized(@"points_pointHistoryTitle")];
    
    self.tableView.estimatedRowHeight = 134;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = UIColor.clearColor;
    [self.tableView registerClass:[BRPointListTbCell class] forCellReuseIdentifier:@"BRPointListTbCell"];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.frame = CGRectMake(0, kSafeAreaTopHeight + 58, kScreenWidth, kScreenHeight - (kSafeAreaTopHeight + 58));
    
    self.sectionView = [[UIView alloc] init];
    self.sectionView.hidden = YES;
    self.sectionView.backgroundColor = UIColor.whiteColor;
    [self.sectionView br_addBaseShadowDown];
    [self.view addSubview:self.sectionView];
    [self.sectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(72 + kSafeAreaTopHeight);
        make.top.offset(0);
        make.left.right.offset(0);
    }];
    
    
    UIView *bfoot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 14)];
    self.tableView.tableFooterView = bfoot;
    
    [self.view addSubview:self.couponTypeView];
    [self.couponTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.right.offset(-14);
        make.top.offset(kSafeAreaTopHeight + 14);
        make.height.mas_equalTo(44);
    }];
    [self.view bringSubviewToFront:navV];
}

- (YGCategoryCustomView *)couponTypeView {
    if (_couponTypeView == nil) {
        _couponTypeView = [[YGCategoryCustomView alloc] initWithFrame:CGRectMake(14, 14, kScreenWidth - 28, 44) titles:@[Localized(@"points_pointRecord"),Localized(@"points_pointUsed"),]];
      
    }
    return _couponTypeView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
